package com.hoongs.v1.utils;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    /**
     * 返回状态码
     */
    public Integer code;
    /**
     * 返回信息
     */
    private String msg;
    /**
     * 返回数据
     */
    private T data;

    public static final int GD_SUCCESS = 200;
    public static final int GD_ERROR = 201;
    public static final int OP_SUCCESS = 300;
    public static final int OP_ERROR = 301;
    public static final int AHTU_ERROR = 401;
    public static final int TOKEN_ERROR = 402;

    public static final String GD_MSG_SUCCESS = "获取数据成功";
    public static final String GD_MSG_ERROR = "获取数据失败";
    public static final String OP_MSG_SUCCESS = "操作成功";
    public static final String OP_MSG_ERROR = "操作失败";
    public static final String AHTU_MSG_ERROR = "权限不足，无法访问！";
    public static final String TOKEN_MSG_ERROR = "没有查询到登录信息！";

    /**
     * 处理抛出异常
     *
     * @return
     */
    public static String resultMsg(BindingResult bindingResult) {
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        List<String> collect = fieldErrors.stream().map(o -> o.getDefaultMessage()).collect(Collectors.toList());
        if (collect.size() > 0) {
            return collect.get(0);
        } else {
            return "";
        }
    }


    /**
     * 获取数据成功
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(GD_SUCCESS);
        result.setMsg(GD_MSG_SUCCESS);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = success();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(String msg) {
        Result<T> result = success();
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> success(String msg, T data) {
        Result<T> result = success();
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> success(Integer code, String msg, T data) {
        Result<T> result = success();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    /**
     * 获取数据失败
     */
    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.setCode(GD_ERROR);
        result.setMsg(GD_MSG_ERROR);
        return result;
    }

    public static <T> Result<T> error(T data) {
        Result<T> result = error();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result<T> result = error();
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> error(String msg, T data) {
        Result<T> result = error();
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> error(Integer code, String msg, T data) {
        Result<T> result = error();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    /**
     * 操作成功
     */
    public static <T> Result<T> opSuccess() {
        Result<T> result = new Result<>();
        result.setCode(OP_SUCCESS);
        result.setMsg(OP_MSG_SUCCESS);
        return result;
    }

    public static <T> Result<T> opSuccess(String msg) {
        Result<T> result = opSuccess();
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> opSuccess(String msg, T data) {
        Result<T> result = opSuccess();
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> opSuccess(Integer code, String msg, T data) {
        Result<T> result = opSuccess();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    /**
     * 操作失败
     */
    public static <T> Result<T> opError() {
        Result<T> result = new Result<>();
        result.setCode(OP_ERROR);
        result.setMsg(OP_MSG_ERROR);
        return result;
    }

    public static <T> Result<T> opError(T data) {
        Result<T> result = opError();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> opError(String msg) {
        Result<T> result = opError();
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> opError(String msg, T data) {
        Result<T> result = opError();
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> opError(Integer code, String msg, T data) {
        Result<T> result = opError();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

}
